package com.scau402.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.scau402.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.scau402.common.annotation.Log;
import com.scau402.common.core.controller.BaseController;
import com.scau402.common.core.domain.AjaxResult;
import com.scau402.common.enums.BusinessType;
import com.scau402.system.domain.WmsJadeType;
import com.scau402.system.service.IWmsJadeTypeService;
import com.scau402.common.utils.poi.ExcelUtil;
import com.scau402.common.core.page.TableDataInfo;

/**
 * 翡翠品类Controller
 * 
 * @author ruoyi
 * @date 2023-06-26
 */
@RestController
@RequestMapping("/system/jadeType")
public class WmsJadeTypeController extends BaseController
{
    @Autowired
    private IWmsJadeTypeService wmsJadeTypeService;

    /**
     * 查询翡翠品类列表
     */
    @PreAuthorize("@ss.hasPermi('system:jadeType:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsJadeType wmsJadeType)
    {
        startPage();
        List<WmsJadeType> list = wmsJadeTypeService.selectWmsJadeTypeList(wmsJadeType);
        return getDataTable(list);
    }

    /**
     * 返回带结构的翡翠品类表
     * @param wmsJadeType
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:jadeType:list')")
    @GetMapping("/simplist")
    public AjaxResult simplist(WmsJadeType wmsJadeType)
    {
        List<WmsJadeType> list = wmsJadeTypeService.selectWmsJadeTypeList(wmsJadeType);
        // 如果租户没有所属翡翠品类，则新建基本品类
        if(list.size() == 0) {
            wmsJadeTypeService.setTenatBasicType(SecurityUtils.getLoginUser().getTenantId());
            list = wmsJadeTypeService.selectWmsJadeTypeList(wmsJadeType);
        }
        List<WmsJadeType> result = wmsJadeTypeService.buildJadeTypeTree(list);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data", result);
        return ajax;
    }

    /**
     * 导出翡翠品类列表
     */
    @PreAuthorize("@ss.hasPermi('system:jadeType:export')")
    @Log(title = "翡翠品类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsJadeType wmsJadeType)
    {
        List<WmsJadeType> list = wmsJadeTypeService.selectWmsJadeTypeList(wmsJadeType);
        ExcelUtil<WmsJadeType> util = new ExcelUtil<WmsJadeType>(WmsJadeType.class);
        util.exportExcel(response, list, "翡翠品类数据");
    }

    /**
     * 获取翡翠品类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:jadeType:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsJadeTypeService.selectWmsJadeTypeById(id));
    }

    /**
     * 新增翡翠品类
     */
    @PreAuthorize("@ss.hasPermi('system:jadeType:add')")
    @Log(title = "翡翠品类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsJadeType wmsJadeType)
    {
        wmsJadeType.setCreateBy(getUsername());
        return toAjax(wmsJadeTypeService.insertWmsJadeType(wmsJadeType));
    }

    /**
     * 修改翡翠品类
     */
    @PreAuthorize("@ss.hasPermi('system:jadeType:edit')")
    @Log(title = "翡翠品类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsJadeType wmsJadeType)
    {
        return toAjax(wmsJadeTypeService.updateWmsJadeType(wmsJadeType));
    }

    /**
     * 删除翡翠品类
     */
    @PreAuthorize("@ss.hasPermi('system:jadeType:remove')")
    @Log(title = "翡翠品类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsJadeTypeService.deleteWmsJadeTypeByIds(ids));
    }

}
